﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CH.Easy.DbProvider.Core;
using System.Linq.Expressions;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace CH.Easy.DbProvider.Provider
{
    public class MsSql2005ExpressionTSQLTranslator : ExpressionTSQLTranslator
    {
      
        protected override Expression VisitConstant(ConstantExpression c)
        {

            IQueryable q = c.Value as IQueryable;
            if (q != null)
            {
                string aliasName = "T" + q.ElementType.GetHashCode();
                this.Append(q.ElementType.Name);
                this.Append(" AS ");
                this.Append(aliasName + " ");
                if (!tableAlias.ContainsKey(aliasName))
                {
                    tableAlias.Add(aliasName, "t" + base.tableIndex++);
                }
            }
            else
            {
                //生成参数 @p1
                string paramName = "@p" + paramIndex++;
                //设置参数
                base.parameter.Add(new SqlParameter(paramName, c.Value));
                this.Append(paramName);
            }
            return c;
        }

        public override string GetIdentitySQL()
        {
            return " SELECT SCOPE_IDENTITY()";
        }
    }
}
